package com.ly.web.controller.erp;

import com.ly.common.utils.StringUtils;
import com.ly.common.utils.erp.ConstUtil;
import com.ly.common.utils.erp.KingConstUtil;
import com.ly.heart.domain.KingFinancePaySettlementBillEntity;
import com.ly.heart.domain.dto.KingFinancePayDto;
import com.ly.heart.service.IKingFinancePayBillService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ly.common.annotation.Log;
import com.ly.common.core.controller.BaseController;
import com.ly.common.core.domain.AjaxResult;
import com.ly.common.enums.BusinessType;
import com.ly.heart.domain.KingFinancePayBillEntity;

import javax.annotation.Resource;

/**
 * 金蝶-财务-应付票据Controller
 *
 * @author wutao
 * @date 2024-06-19
 */
@Api ( value = "KingFinancePayBill", tags = "金蝶-财务-应付票据" )
@RestController
@RequestMapping ( "/heart/KingFinancePayBill" )
public class KingFinancePayBillController extends BaseController {

    @Resource
    private IKingFinancePayBillService kingFinancePayBillEntityService;

    /**
     * 查询金蝶-财务-应付票据列表
     */
    @PreAuthorize ( "@ss.hasPermi('heart:KingFinancePayBill:list')" )
    @GetMapping ( "/list" )
    @ApiOperation ( "查询金蝶-财务-应付票据列表" )
    public AjaxResult list ( KingFinancePayBillEntity kingFinancePayBillEntity ) {
        AjaxResult ajaxResult = kingFinancePayBillEntityService.selectKingFinancePayBillEntityList ( kingFinancePayBillEntity );
        return ajaxResult;
    }

    /**
     * 获取金蝶-财务-应付票据详细信息
     */
    @PreAuthorize ( "@ss.hasPermi('heart:KingFinancePayBill:query')" )
    @GetMapping ( value = "/{id}" )
    @ApiOperation ( "获取金蝶-财务-应付票据详细信息" )
    public AjaxResult getInfo ( @PathVariable ( "id" ) String id ) {
        return AjaxResult.success ( kingFinancePayBillEntityService.selectKingFinancePayBillEntityById ( id ) );
    }

    /**
     * 新增金蝶-财务-应付票据
     */
    @PreAuthorize ( "@ss.hasPermi('heart:KingFinancePayBill:add')" )
    @Log ( title = "金蝶-财务-应付票据", businessType = BusinessType.INSERT )
    @PostMapping ( "draft" )
    @ApiOperation ( "新增金蝶-财务-应付票据" )
    public AjaxResult draft ( @RequestBody KingFinancePayBillEntity kingFinancePayBillEntity ) {
        if ( StringUtils.isNotBlank ( kingFinancePayBillEntity.getId () ) ) {
            return kingFinancePayBillEntityService.updateKingFinancePayBillEntity ( kingFinancePayBillEntity );
        }
        return kingFinancePayBillEntityService.draft ( kingFinancePayBillEntity );
    }

    @PreAuthorize ( "@ss.hasPermi('heart:KingFinancePayBill:add')" )
    @Log ( title = "金蝶-财务-应付票据", businessType = BusinessType.INSERT )
    @PostMapping ( "submit" )
    @ApiOperation ( "新增金蝶-财务-应付票据" )
    public AjaxResult submit ( @RequestBody KingFinancePayBillEntity kingFinancePayBillEntity ) {
        return kingFinancePayBillEntityService.submit ( kingFinancePayBillEntity );
    }

    /**
     * 修改金蝶-财务-应付票据
     */
    @PreAuthorize ( "@ss.hasPermi('heart:KingFinancePayBill:edit')" )
    @Log ( title = "金蝶-财务-应付票据", businessType = BusinessType.UPDATE )
    @PutMapping
    @ApiOperation ( "修改金蝶-财务-应付票据" )
    public AjaxResult edit ( @RequestBody KingFinancePayBillEntity kingFinancePayBillEntity ) {
        return kingFinancePayBillEntityService.updateKingFinancePayBillEntity ( kingFinancePayBillEntity );
    }

    /**
     * 查询应付票据状态，决定是否可以操作业务
     *
     * @return
     */
    //@PreAuthorize ("@ss.hasPermi('heart:KingFinancePayBill:queryStatus')")
    @PostMapping ( "queryStatus" )
    @ApiOperation ( "获取金蝶-财务-查询应付票据状态" )
    public AjaxResult queryStatus ( @RequestBody KingFinancePayBillEntity kingFinancePayBillEntity ) {
        KingFinancePayBillEntity payBillEntity = kingFinancePayBillEntityService.getById ( kingFinancePayBillEntity );
        if ( payBillEntity == null ) {
            return AjaxResult.error ( "该票据不存在" );
        }
        if ( ! ConstUtil.STATE_FINISH.equals ( payBillEntity.getState () ) ) {
            return AjaxResult.error ( "选中的单据还没有审核，不能进行操作！" );
        }
        if ( payBillEntity.getSettlementStatus () != KingConstUtil.FINANCE_PAY_BILL_SETTLEMENT_STATUS_EMPTY ) {
            return AjaxResult.error ( "单据已经到期付款或者退票，不能执行操作！" );
        }
        return AjaxResult.success ();
    }

    /**
     * 应付票据到期付款
     * 1、修改应付票据状态
     * 2、自动生成应付票据结算单
     */
    @PreAuthorize ( "@ss.hasPermi('heart:KingFinancePayBill:pay')" )
    @Log ( title = "金蝶-财务-应付票据-到期付款", businessType = BusinessType.UPDATE )
    @PostMapping ( "pay" )
    @ApiOperation ( "修改金蝶-财务-应付票据-到期付款" )
    public AjaxResult pay ( @RequestBody KingFinancePaySettlementBillEntity kingFinancePaySettlementBillEntity ) {
        return kingFinancePayBillEntityService.pay ( kingFinancePaySettlementBillEntity );
    }

    /**
     * 应付票据退款操作
     * 生成付款退款单
     *
     * @param kingFinancePaySettlementBillEntity
     * @return
     */
    @PreAuthorize ( "@ss.hasPermi('heart:KingFinancePayBill:refund')" )
    @Log ( title = "金蝶-财务-应付票据-退票", businessType = BusinessType.UPDATE )
    @PostMapping ( "refund" )
    @ApiOperation ( "修改金蝶-财务-应付票据-退票" )
    public AjaxResult refund ( @RequestBody KingFinancePaySettlementBillEntity kingFinancePaySettlementBillEntity ) {
        AjaxResult refund = kingFinancePayBillEntityService.refund ( kingFinancePaySettlementBillEntity );
        return refund;
    }

    /**
     * 删除金蝶-财务-应付票据
     */
    @PreAuthorize ( "@ss.hasPermi('heart:KingFinancePayBill:remove')" )
    @Log ( title = "金蝶-财务-应付票据", businessType = BusinessType.DELETE )
    @DeleteMapping ( "/{ids}" )
    @ApiOperation ( "删除金蝶-财务-应付票据" )
    public AjaxResult remove ( @PathVariable String[] ids ) {
        return kingFinancePayBillEntityService.deleteKingFinancePayBillEntityByIds ( ids );
    }
}
